from datetime import datetime, timedelta
import time

from flask import abort, jsonify
from flask import current_app
from flask import g
from flask import redirect
from flask import render_template
from flask import request
from flask import session
from flask import url_for

from info.constants import USER_COLLECTION_MAX_NEWS, QINIU_DOMIN_PREFIX
from info.models import User, News, Category
from info.modules.admin import admin_blu

# 后台首页
from info.utils.common import user_login_data, file_upload
from info.utils.response_code import RET
from info.utils.response_code import error_map


@admin_blu.route('/', methods=['GET', 'POST'])
@user_login_data
def index():
    return render_template("admin/index.html", user=g.user.to_dict())


# 后台登录
@admin_blu.route('/login', methods=['POST', 'GET'])
def login():
    if request.method == 'GET':  # 展示页面
        # 取出session中的数据, 判断登录状态
        user_id = session.get("user_id")
        is_admin = session.get("is_admin")
        if user_id and is_admin:  # 管理员已登录, 重定向到后台首页
            return redirect(url_for("admin.index"))

        return render_template("admin/login.html")

    # POST 处理 获取参数
    username = request.form.get("username")
    password = request.form.get("password")
    # 校验参数
    if not all([username, password]):
        return render_template("admin/login.html", errmsg="参数不足")

    # 查询用户模型  必须是管理员
    try:
        user = User.query.filter(User.mobile == username, User.is_admin == True).first()
    except BaseException as e:
        current_app.logger.error(e)
        return render_template("admin/login.html", errmsg="数据库错误")

    if not user:
        return render_template("admin/login.html", errmsg="用户不存在")

    # 校验密码
    if not user.check_password(password):
        return render_template("admin/login.html", errmsg="用户/密码错误")

    # 状态保持, 记录用户信息
    session["user_id"] = user.id
    session["is_admin"] = True
    # 如果成功, 则重定向到后台首页
    return redirect(url_for("admin.index"))


# 用户退出
@admin_blu.route('/logout')
def logout():
    # 删除session中的数据
    session.pop("user_id", None)
    session.pop("is_admin", None)
    return redirect(url_for("admin.login"))


# 用户统计
@admin_blu.route('/user_count')
def user_count():
    # 用户总数
    total_count = 0
    try:
        total_count = User.query.filter(User.is_admin == False).count()
    except BaseException as e:
        current_app.logger.error(e)

    # 获取当前日期对应的年和月
    t = time.localtime()
    # 构建日期字符串  "2018-11-01"
    mon_date_str = "%d-%02d-01" % (t.tm_year, t.tm_mon)
    # 将日期字符串 转为 日期对象
    mon_date = datetime.strptime(mon_date_str, "%Y-%m-%d")

    # 月新增人数  注册时间 >= 本月1号0点
    mon_count = 0
    try:
        mon_count = User.query.filter(User.is_admin == False, User.create_time >= mon_date).count()
    except BaseException as e:
        current_app.logger.error(e)

    # 日新增人数  注册时间 >= 本年本月本日0点
    day_count = 0
    # 构建日期字符串   取出当前时间对应的年 月 日
    day_date_str = "%d-%02d-%02d" % (t.tm_year, t.tm_mon, t.tm_mday)
    # 转为日期对象
    day_date = datetime.strptime(day_date_str, "%Y-%m-%d")
    try:
        day_count = User.query.filter(User.is_admin == False, User.create_time >= day_date).count()
    except BaseException as e:
        current_app.logger.error(e)

    # 日注册人数统计  取某日的注册人数   >= 当日0点, < 次日0点
    active_count = []
    active_time = []
    for i in range(0, 30):
        begin_date = day_date - timedelta(days=i)  # 当日0点
        end_date = day_date + timedelta(days=1) - timedelta(days=i)  # 次日0点
        try:
            one_day_count = User.query.filter(User.is_admin == False, User.create_time >= begin_date,
                                              User.create_time < end_date).count()
            active_count.append(one_day_count)

            # 获取当日日期对象对应的字符串形式
            one_day_str = begin_date.strftime("%Y-%m-%d")
            active_time.append(one_day_str)  # ["2018-11-19"]

        except BaseException as e:
            current_app.logger.error(e)

    # 列表倒序
    active_time.reverse()
    active_count.reverse()

    data = {
        "total_count": total_count,
        "mon_count": mon_count,
        "day_count": day_count,
        "active_count": active_count,
        "active_time": active_time
    }
    return render_template("admin/user_count.html", data=data)


@admin_blu.route('/user_list')
def user_list():
    # 获取参数
    p = request.args.get("p", 1)
    # 校验参数
    try:
        p = int(p)
    except BaseException as e:
        current_app.logger.error(e)
        return abort(403)

    # 查询所有的用户
    try:
        pn = User.query.filter(User.is_admin == False).paginate(p, USER_COLLECTION_MAX_NEWS)
    except BaseException as e:
        current_app.logger.error(e)
        return abort(500)

    data = {
        "user_list": [user.to_admin_dict() for user in pn.items],
        "cur_page": pn.page,
        "total_page": pn.pages
    }
    # 传入模板渲染
    return render_template("admin/user_list.html", data=data)


# 新闻审核列表
@admin_blu.route('/news_review')
def news_review():
    # 获取参数
    p = request.args.get("p", 1)
    keyword = request.args.get("keyword")
    # 校验参数
    try:
        p = int(p)
    except BaseException as e:
        current_app.logger.error(e)
        return abort(403)

    filter_list = []
    # 判断是否设置了关键字, 如果设置则添加过滤条件
    if keyword:
        filter_list.append(News.title.contains(keyword))

    # 查询所有的新闻
    try:
        pn = News.query.filter(*filter_list).paginate(p, USER_COLLECTION_MAX_NEWS)
    except BaseException as e:
        current_app.logger.error(e)
        return abort(500)

    data = {
        "news_list": [news.to_review_dict() for news in pn.items],
        "cur_page": pn.page,
        "total_page": pn.pages
    }
    # 传入模板渲染
    return render_template("admin/news_review.html", data=data)


# 新闻审核详情
@admin_blu.route('/news_review_detail/<int:news_id>')
def news_review_detail(news_id):
    # 根据新闻id查询新闻
    try:
        news = News.query.get(news_id)
    except BaseException as e:
        current_app.logger.error(e)
        return abort(500)

    # 传入模板渲染
    return render_template("admin/news_review_detail.html", news=news.to_dict())


# 新闻审核
@admin_blu.route('/news_review_action', methods=['POST'])
def news_review_action():
    # 获取参数
    news_id = request.json.get("news_id")
    action = request.json.get("action")
    # 校验参数
    if not all([news_id, action]):
        return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR])
    if action not in ["accept", "reject"]:
        return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR])

    try:
        news_id = int(news_id)
    except BaseException as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR])

    # 查询新闻模型
    try:
        news = News.query.get(news_id)
    except BaseException as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR])

    # 修改新闻的状态
    if action == "accept":
        news.status = 0
    else:
        reason = request.json.get("reason")
        if reason:
            news.reason = reason
        news.status = -1

    # json返回结果
    return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])


# 新闻编辑列表
@admin_blu.route('/news_edit')
def news_edit():
    # 获取参数
    p = request.args.get("p", 1)
    keyword = request.args.get("keyword")
    # 校验参数
    try:
        p = int(p)
    except BaseException as e:
        current_app.logger.error(e)
        return abort(403)

    filter_list = []
    # 判断是否设置了关键字, 如果设置则添加过滤条件
    if keyword:
        filter_list.append(News.title.contains(keyword))

    # 查询所有的新闻
    try:
        pn = News.query.filter(*filter_list).paginate(p, USER_COLLECTION_MAX_NEWS)
    except BaseException as e:
        current_app.logger.error(e)
        return abort(500)

    data = {
        "news_list": [news.to_review_dict() for news in pn.items],
        "cur_page": pn.page,
        "total_page": pn.pages
    }
    # 传入模板渲染
    return render_template("admin/news_edit.html", data=data)


# 新闻编辑详情
@admin_blu.route('/news_edit_detail')
def news_edit_detail():
    # 获取参数
    news_id = request.args.get("news_id")
    # 校验参数
    try:
        news_id = int(news_id)
    except BaseException as e:
        current_app.logger.error(e)
        return abort(403)

    # 根据新闻id查询新闻数据
    try:
        news = News.query.get(news_id)
    except BaseException as e:
        current_app.logger.error(e)
        return abort(500)

    # 查询所有的分类数据
    try:
        categories = Category.query.filter(Category.id != 1).all()
    except BaseException as e:
        current_app.logger.error(e)
        return abort(500)

    # 标识新闻对应的分类
    category_list = []
    for category in categories:
        is_selected = False
        category_dict = category.to_dict()
        if category.id == news.category_id:
            is_selected = True
        category_dict['is_selected'] = is_selected
        category_list.append(category_dict)

    # 传入模板渲染
    return render_template("admin/news_edit_detail.html", news=news.to_dict(), category_list=category_list)


# 新闻编辑
@admin_blu.route('/news_edit_detail', methods=['POST'])
def news_edit_action():
    # 获取参数
    news_id = request.form.get("news_id")
    title = request.form.get("title")
    category_id = request.form.get("category_id")
    digest = request.form.get("digest")
    content = request.form.get("content")
    img_file = request.files.get("index_image")
    # 校验参数
    if not all([news_id, title, category_id, digest, content]):
        return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR])

    try:
        category_id = int(category_id)
        news_id = int(news_id)
    except BaseException as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR])
    # 查询该新闻数据
    try:
        news = News.query.get(news_id)
    except BaseException as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR])

    # 修改新闻数据
    news.title = title
    news.category_id = category_id
    news.digest = digest
    news.content = content
    if img_file:  # 修改了图片
        try:
            img_bytes = img_file.read()
            file_name = file_upload(img_bytes)
            news.index_image_url = QINIU_DOMIN_PREFIX + file_name
        except BaseException as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.THIRDERR, errmsg=error_map[RET.THIRDERR])

    # json返回结果
    return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])